package sk.tuke.android4all.dialer;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnGenericMotionListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import sk.tuke.android4all.controls.CustomFragment;
import sk.tuke.android4all.listeners.SwipeListener;
import sk.tuke.android4all.services.Tts;

public class ContactsFragment extends CustomFragment {

	private static String TAG = "ContactsFragment";
	private ContactsAdapter mContacts = null;
	private Activity mActivity = null;
	private String selectedTag = null;
	int touchCounter = 0;
	private final int TOUCH_INTENSITY = 4;
	private SwipeListener mActionListener = null;
	private Tts mSpeechEngine;
	
	public ContactsFragment(Activity aActivity_in){
		this.mActivity = aActivity_in;
		mContacts = new ContactsAdapter(mActivity.getApplicationContext());
		mSpeechEngine = new Tts(mActivity);
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);	
        
        return rootView;
    }
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       createUI();
    }
	
	private void createUI() {
		mActionListener =  new SwipeListener(mActivity) {
            @Override
            public void onDoubleTapAction(int count) {
                Log.i("TAG", "DoubleTapped " + selectedTag);
                resetTouchCounter();
                if(selectedTag != null){
                	if(selectedTag == getResources().getString(R.string.fragmentcontacts_next)){
                		showNextItems();
                	}else{
                		mSpeechEngine.sayMessage(mContacts.getContactInfo(selectedTag).getName());
                		showContextMenuforItem(selectedTag);
                	}
                }else{
                	showContextMenuforItem(null);
                }
            }

			@Override
			public void onZoomInAction() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onZoomOutAction() {
				// TODO Auto-generated method stub
				
			}
		};
		getView().setOnTouchListener(mActionListener);
		
		resetItems();
		Log.d(TAG, "Scrool needend" + String.valueOf(mContacts.scrollNeeded()));
		if(mContacts.scrollNeeded()){
			setControl(R.id.fragmentcontacts_next, null, true);
		}else{
			((LinearLayout) getView()).removeView(getView().findViewById(R.id.fragmentcontacts_next));
		}
		if(mContacts.isEmpty()){
			mSpeechEngine.sayMessage(getResources().getString(R.string.fragmentcontacts_noitem));
		}
	}
	
	private void showNextItems() {
		mContacts.ScrollDown();
		resetItems();
	}
	
	private void showContextMenuforItem(String selectedTag) {
		mActivity.getFragmentManager().beginTransaction().replace(R.id.container, new ContextmenuFragment(mActivity, mContacts.getContactInfo(selectedTag)),ContextmenuFragment.class.getName()).commit();		
	}
	
	private void resetItems(){
		ContactInfo[] aContacts = mContacts.getCurrentSet();
		setControl(R.id.fragmentcontacts_item1, aContacts[0], false);
		setControl(R.id.fragmentcontacts_item2, aContacts[1], false);
		setControl(R.id.fragmentcontacts_item3, aContacts[2], false);
		setControl(R.id.fragmentcontacts_item4, aContacts[3], false);
		setControl(R.id.fragmentcontacts_item5, aContacts[4], false);
	}

	private void setControl(int controlId,final ContactInfo contact, boolean isNavButton) {
		final TextView controlTv = (TextView) getView().findViewById(controlId);
		if(contact != null){
			controlTv.setText(contact.getName());	
		}else{
			if(!isNavButton){
				((LinearLayout) getView()).removeView(controlTv);
				return;
			}
		}
		controlTv.setOnGenericMotionListener(new OnGenericMotionListener(){

			@Override
			public boolean onGenericMotion(View arg0, MotionEvent arg1) {
				mActionListener.resetTouchCounter();
				String textInControl = controlTv.getText().toString();
				String actionString = contact!=null?contact.getId(): controlTv.getText().toString();
                if (selectedTag != actionString && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    mSpeechEngine.sayMessage(textInControl);
                    if(contact!=null){
                    	selectedTag = contact.getId();
                    }else{
                    	selectedTag = controlTv.getText().toString();
                    }
                }else{
                    touchCounter++;
                }
                return false;
			}});		
	}

	@Override
	public void returnAction() {
		mSpeechEngine.sayMessage(getResources().getString(R.string.fragmentdialer_intro));
		mActivity.getFragmentManager().beginTransaction().replace(R.id.container, new DialerFragment(mActivity), DialerFragment.class.getName()).commit();
	}

}
